$(function () {
    var $templateTr = $("#templateTr").clone().removeAttr("id");
    $("#templateTr").parent("table").remove();
    $("#addRow").on("click", function () {
        let $tr = $templateTr.clone();
        $("select", $tr).addClass("select2");
        window.$nojs($tr);
        $("#tbody").append($tr);
    }).trigger("click");
});

/**
 * 修改某一行数据
 * @param id
 */
function clickUpdate(id){
    alert("id :" + id);
}

/**
 * 批量新增
 * @param event
 */
function submitBatch(event) {
    let $trs = $("#tbody tr");
    let datas = [];
    for (let i = 0; i < $trs.length; i++) {
        let $tr = $trs.eq(i);
        let data = {
            projectId: $(":input[name='projectId']", $tr).val(),
            jira: $("input[name='jira']", $tr).val(),
            duration: $("input[name='duration']", $tr).val(),
            content: $(":input[name='content']", $tr).val(),
            date: $(":input[name='date']", $tr).val(),
        }
        if (!common.validateForm(data)) {
            $.alert("请完整填写工时内容");
            return;
        }
        datas.push(data);
    }
    let url = ctx + "report/batchInsert";
    $.ajax(url, {
        dataType: "json",
        type: "post",
        data: JSON.stringify(datas),
        contentType: "application/json",
        success: function (data) {
            if (data.code == 0) {
                $.alert({
                    title: '成功',
                    content: '批量新增成功'

                });
                $("#backBtn")[0].click();
            } else {
                $.alert({
                    title: '失败',
                    content: data.msg
                });
            }
        }

    })

}

function addRowReport(event) {
    let $btn = $(event);
    debugger;
    let $tr = $btn.parents("tr");
    let data = {
        projectId: $(":input[name='projectId']", $tr).val(),
        jira: $("input[name='jira']", $tr).val(),
        duration: $("input[name='duration']", $tr).val(),
        content: $(":input[name='content']", $tr).val(),
        date: $("#day").val()
    };
    checkAndSave(data);

}

function delRowReport(event) {
    $.confirm({
        title: '删除工时',
        content: '确定删除待新增工时吗？',
        columnClass: 'col-md-8 col-md-offset-2 ',
        buttons: {
            cancel: function () {
            },
            confirm: {
                text: '确定',
                btnClass: 'btn-blue',
                action: function () {
                    getRow(event).remove();
                    summaryTotalTimes();

                }
            }
        }
    })
}

function getRow(event) {
    return $(event).parents("tr");
}

function fillRowJiraPrefix(event) {
    let prefix = $(event).find("option:selected").data("jiraPrefix");
    getRow(event).find(":input[name='jira']").val(prefix);
}

function checkRowProject(event) {
    let projectId = getRow(event).find(":input[name='projectId']").val();
    if (!projectId) {
        $.alert('请先选择项目');
    }
}

function checkRowJiraPrefix(event) {
    debugger
    let jira = event.value;
    let prefix = getRow(event).find("option:selected").data("jiraPrefix");
    if (jira && prefix) {
        if (!jira.startsWith(prefix)) {
            $.alert('jira号须符合项目的jira前缀要求!');
            $("#jira").val("");
            return false;
        }
    }
    // 同时检测jira号是否存在于jira系统
    if (jira) {
        let exist = common.checkJiraExist(jira);
        if (!exist) {
            $.alert({
                type: 'red',
                title: '',
                content: '注意: <strong style="color: red">' +jira +'</strong>不存在于jira系统'
            });
        }
    }
}



$("#addReport").on("click", function () {
    $.confirm({
        title: '新增工时',
        content: 'url:' + ctx + "report/addForm/" + $("#day").val(),
        columnClass: 'col-md-12 ',
        buttons: {
            取消: function () {
            },
            confirm: {
                text: '确定',
                btnClass: 'btn-blue',
                action: function () {
                    let formJson = this.$content.find("form").formJson();
                    checkAndSave(formJson);
                }
            }
        }
    });

})

function checkAndSave(formJson) {
    if (!common.validateForm(formJson)) {
        $.alert('请完整填写工时内容');
        return false;
    }
    let exist = common.checkJiraExist(formJson.jira);
    if (!exist) {
        $.confirm({
            title: 'jira不存在',
            content: 'jira号不存在于jira系统，确认提交吗?',
            buttons: {
                确定: function () {
                    doSave(formJson);
                },
                取消: function () {
                }

            }
        });
        return false
    } else {
        doSave(formJson);
        return true;
    }

}

function doSave(data) {
    let url = "report/addForm/save";
    $request.post(url, {data: data}, function (data) {
        debugger;
        if (data.success) {
            $.alert({
                title: '操作成功',
                content: '',
                onClose: function () {
                    window.location.reload();
                }
            });
        } else {
            $.alert(data.msg);
        }
    })
}

/**
 * 累计当前填报工时
 */
function summaryTotalTimes(){
    let sum = 0; // 初始化累加器
    $($("#tbody td input[name='duration']")).each(function() {
        // input元素的值是数字
        let value = parseInt($(this).val(), 0);
        if (!isNaN(value)) {
            sum += value; // 累加数值
        }
    });
    console.info(sum)
    $("#totalTimes").text(sum);

}